package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.CrazyCompetition;
import com.ltmall.api.bean.CrazyCompetitionMember;
import com.ltmall.api.bean.WalletGiftCard;
import com.ltmall.api.dao.ICrazyDAO;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;

import java.sql.SQLException;
import java.util.List;

import static com.ltmall.api.util.HibernateUtil.getSession;

public class CrazyDAO extends BaseEntityDAO<CrazyCompetition> implements ICrazyDAO {

    @Override
    public boolean addMember(CrazyCompetitionMember member) {
        member.setOpTime(System.currentTimeMillis());
        try {
            getHibernateTemplate().persist(member);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public int maxPhase() {
        String hql = "select max(cc.phase) from CrazyCompetition cc";
        // LT 修改获取session的方式
        // Query query = this.getSession().createQuery(hql);

        Session session = getSession();
        Query query = session.createQuery(hql);
        Object object = query.uniqueResult();
        if (object == null) {
            return 0;
        } else {
            return (Integer) query.uniqueResult();
        }
    }

    @Override
    @SuppressWarnings("rawtypes")
    public CrazyCompetition getCompetition(int phase) {
        List list = this.getHibernateTemplate().find(
                "from CrazyCompetition  cc where cc.phase =?",
                new Object[]{phase});
        if (list.size() > 0) {
            return (CrazyCompetition) list.get(0);
        }
        return null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<CrazyCompetitionMember> getCompetitionMember(int phase, int cardID, int station) {
        //1：表示交钱的；0：表示未交钱
        String hql;
        //所有的
        if (station == 0) {
            hql = "from CrazyCompetitionMember ccm where ccm.phase =? and ccm.giftCardID = ? ";
        } else {
            hql = "from CrazyCompetitionMember ccm where ccm.phase =? and ccm.giftCardID = ? and ccm.station=1";
        }
        // LT 设置了类型强转
        List<CrazyCompetitionMember> list = (List<CrazyCompetitionMember>) this.getHibernateTemplate().find(hql, new Object[]{phase, cardID});
        return list;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public WalletGiftCard getCard(int cardID) {
        List list = this.getHibernateTemplate().find("from WalletGiftCard  wc where wc.cardID =?", new Object[]{cardID});
        if (list.size() > 0) {
            return (WalletGiftCard) list.get(0);
        }
        return null;
    }

    @Override
    public CrazyCompetitionMember getCompetitionMember(int number) {
        List list = this.getHibernateTemplate().find("from CrazyCompetitionMember  ccm where ccm.number =?",
                new Object[]{number});
        if (list.size() > 0) {
            return (CrazyCompetitionMember) list.get(0);
        }
        return null;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public int getCompetitionNuber(int phase, int cardID, int userID) {
        List list = this.getHibernateTemplate().find(
                "from CrazyCompetitionMember ccm where ccm.phase =? and ccm.giftCardID = ? and ccm.userID = ?",
                new Object[]{phase, cardID, userID});
        if (list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public List<?> getCompetitionMemberWithPhoto(final int phase, final int cardID) {
        final String SQL = "SELECT MEMBER.USER_ID,MEMBER.USER_PHONE,MEMBER.OPTIME,INFO.PHOTO,MEMBER.CHANCE"
                + " from FLAVOR_CRAZYCOMPETITION_MEMBER MEMBER left join FLAVOR_CLIENT_INFO INFO on MEMBER.USER_ID = INFO.USER_ID"
                + " where MEMBER.PHASE = ? AND MEMBER.GIFTCARD_ID = ? order by MEMBER.OPTIME  DESC ";
        List<?> list = this.getHibernateTemplate().execute(
                new HibernateCallback<List>() {
                    public List doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = session.createSQLQuery(SQL);
                        query.setParameter(0, phase);
                        query.setParameter(1, cardID);
                        return query.list();
                    }
                });
        return list;
    }

    @Override
    public boolean updateWinner(int phase, String cardID, String winner) {
        String hql = "update CrazyCompetition cc set cc.winnersID = ? where cc.phase = ? and cc.giftCardID = ?";
        try {
            Session session = getSession();
            Query query = session.createQuery(hql);
            query.setParameter(0, winner);
            query.setParameter(1, phase);
            query.setParameter(2, cardID);
            int number = query.executeUpdate();
            if (number == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updatePhaseStation(int phase, int station) {
        String hql = "update CrazyCompetition cc set cc.station = ? where cc.phase = ?";
        try {
            Session session = getSession();
            Query query = session.createQuery(hql);
            query.setParameter(0, station);
            query.setParameter(1, phase);
            int number = query.executeUpdate();
            if (number == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateGiftCardID(int userID, int cardID) {
        // TODO Auto-generated method stub
        String hql = "update WalletGiftCard card set card.userID = ?, card.buyer = ? where card.cardID=?";
        try {
            Session session = getSession();
            Query query = session.createQuery(hql);
            query.setParameter(0, userID);
            query.setParameter(1, userID);
            query.setParameter(2, cardID);
            int number = query.executeUpdate();
            if (number == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateStationMember(int number, int station) {
        // TODO Auto-generated method stub
        String hql = "update CrazyCompetitionMember mem set mem.station = ? where mem.number=?";
        try {
            Query query = getSession().createQuery(hql);
            query.setParameter(0, station);
            query.setParameter(1, number);
            int num = query.executeUpdate();
            if (num == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<CrazyCompetitionMember> getOneCompetitionMember(int cardID, int userID) {
        // 1：表示交钱的；0：表示未交钱
        String hql;
        hql = "from CrazyCompetitionMember ccm where ccm.giftCardID = ? and ccm.userID = ? and ccm.station!=0";
        // LT 设置了强转
        List<CrazyCompetitionMember> list = (List<CrazyCompetitionMember>) this.getHibernateTemplate().find(
                hql, new Object[]{cardID, userID});
        return list;
    }

}
